﻿using OpenInterface;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text.RegularExpressions;

namespace OpenImplement
{
    /// <summary>
    /// 博客园抓取
    /// </summary>
    public class AnalyzeCnblogs : IAnalyzer
    {
        public void Analyze(string url, string html, Action<string, HandlerType> actHandlerUrl)
        {
            /*
             * 匹配所有的 地址 放到队列继续进一步采集
             */
            Regex reg = new Regex(@"(?is)<a[^>]*?href=(['""\s]?)(?<href>[^'""\s]*)\1[^>]*?>");
            if (reg.IsMatch(html))
            {
                var uri = new Uri(url);
                var matches = reg.Matches(html);
                foreach (Match match in matches)
                {
                    var value = match.Groups["href"].Value;
                    if (value.Equals("/")) continue;
                    //不是http 开头的话，我们要把链接拼上
                    if(!value.StartsWith("http"))
                    {
                        //从根路径开始
                        if(value.StartsWith("/"))
                        {
                            value = string.Format("{0}://{1}{2}", uri.Scheme, uri.Host, value);
                        }
                        else
                        {
                            var Segments = uri.Segments.Skip(1);
                            value = string.Format("{0}/{1}", url.TrimEnd('/'), value.TrimStart('/'));
                        }                        
                    }
                    if (value.Contains("/#") || value.Contains("/javascript")) continue;
                    //测试只采集某个人的
                    if(value!=null&&value.Contains("www.cnblogs.com/zhanggaoxing/p/"))
                        actHandlerUrl.Invoke(value, HandlerType.Default);
                }
            }
        }
    }
}
